/**
 * td.Army构造函数
 * config包括如下属性
 * speed, 速度
 * hp, 血量
 * fly, 是否飞行单位
 * shape, 形状
 * color, 颜色
 * size, 大小
 */
goog.provide('td.Army');

goog.require('Stefan.Rect');
goog.require('Stefan.Circle');

td.Army = T.lang.createClass(function(config) {
    Stefan.Circle.call(this);
    this.config = config || {};
    
    var type = config.type === undefined?0:config.type;
    this.cfg = td.Army.TYPE[config.type];
    this.hp = this.cfg.hp;
    this.damage = this.cfg.damage;
    
    this.grid = config.grid || null;
    this.nextGrid = null;
    this.index = 0;
	this.map = null;
	this.way = [];
	this.direction = td.Army.DIRECTION.RIGHT;
	this.pX = this.grid.pX || 0;
	this.pY = this.grid.pY || 0;
	this.init();
	this.setAnchor(.5, .5);
	this.setPosition(this.pX, this.pY);
	this.hpBar = new Stefan.Rect().setAnchor(0, 1).setFill('#FF0000').setSize(this.cfg.size, 0.2 * td.GRIDSIZE).
	setPosition(-0.5 * this.cfg.size, -0.5 * this.cfg.size);
	
	this.hpBar.width = this.cfg.size;
	this.hpBar.height = 0.2 * td.GRIDSIZE;
	
	
	this.appendChild(this.hpBar);
}, {
	superClass: Stefan.Circle
});

td.Army.DIRECTION = {
	UP: 0,
	RIGHT: 1,
	DOWN: 2,
	LEFT: 3
};

td.Army.TYPE = [
	{
		index: 0,
		name: "small",
		speed: 0.05,
		hp: 100,
		damage: 1,
		money: 1,
		url: 'img/0ball.gif',
		size: 30
	},
	{
		index: 1,
		name: "small",
		speed: 0.05,
		hp: 100,
		damage: 1,
		money: 2,
		url: 'img/1ball.gif',
		size: 30
	},
	{
		index: 2,
		name: "small",
		speed: 0.05,
		hp: 100,
		damage: 1,
		money: 3,
		url: 'img/2ball.gif',
		size: 30
	},
	{
		index: 3,
		name: "small",
		speed: 0.05,
		hp: 100,
		damage: 2,
		money: 4,
		url: 'img/3ball.gif',
		size: 30
	}
];

td.Army.extend({
	init: function(){
		this.setSize(this.cfg.size, this.cfg.size).setFill(this.cfg.url);
	},
	
	getNextGrid: function () {
		if (this.way.length == 0) {
			this.findWay();
		}
		var ng = this.way.shift();
		if(!ng) return;
		var nextGrid = this.grid.map.getGridByCoor(ng[0], ng[1]);
		if (nextGrid && (!nextGrid.passable || nextGrid.building)) {
			this.findWay();
			ng = this.way.shift();
			nextGrid = this.grid.map.getGridByCoor(ng[0], ng[1]);
		}
		return nextGrid;
	},
	
	findWay: function () {
		var __this = this;
		fw = new td.FindWay(
			this.grid.map.rows, this.grid.map.columns,
			Math.floor(this.grid.pX/td.GRIDSIZE), Math.floor(this.grid.pY/td.GRIDSIZE),
			this.grid.map.exitPosition[0], this.grid.map.exitPosition[1],
			function (x, y) {
				var currentGrid = __this.grid.map.getGridByCoor(x, y);
				return !currentGrid.building && currentGrid.passable;
			}
		);
		this.way = fw.way;
		delete fw;
	},
	
	getDirection: function () {
		if (!this.grid || !this.nextGrid) return;
		if (this.grid.dY < this.nextGrid.dY) {
			this.direction = td.Army.DIRECTION.UP;
		} else if (this.grid.dX < this.nextGrid.dX) {
			this.direction = td.Army.DIRECTION.RIGHT;
		} else if (this.grid.dY > this.nextGrid.dY) {
			this.direction = td.Army.DIRECTION.DOWN;
		} else if (this.grid.dX > this.nextGrid.dX) {
			this.direction = td.Army.DIRECTION.LEFT;
		}
	},
	
	
	arrive: function () {
		if (this.grid && 
				Math.floor(this.grid.pY/td.GRIDSIZE) == this.grid.map.exitPosition[1] && 
				Math.floor(this.grid.pX/td.GRIDSIZE) == this.grid.map.exitPosition[0]) {
			td.GAME.life -= this.damage;
			if (td.GAME.life <= 0) {
				td.GAME.life = 0;
			} else {
				this.destory();
			}
			td.GAME.controller.updateLife();
		}
	},
	
	destory: function(){
		var alen = this.grid.map.armyArr.length;
		if(this.index !== alen - 1){
			for(var i = this.index + 1; i < alen; i++){
				this.grid.map.armyArr[i].index--;
			}
		}
		this.grid.map.armyArr.splice(this.index, 1);
		this.grid.map.removeChild(this);
		this.isDead = true;
		delete this;
	},
	
	toNext: function () {
		this.grid = this.nextGrid;
		this.nextGrid = null;
		this.arrive();
	},
	
	step: function () {
		if (!this.grid) return;
	
		if (!this.nextGrid) {
			this.nextGrid = this.getNextGrid();
			if (!this.nextGrid) {
				this.isBlocked = true;
				return;
			}
		}
		
	
		if (this.pX == this.nextGrid.pX && this.pY == this.nextGrid.pY) {
			this.toNext();
		} else {
			var distanceX = this.nextGrid.pX - this.pX,
				distanceY = this.nextGrid.pY - this.pY,
				sx = distanceX < 0 ? -1 : 1,
				sy = distanceY < 0 ? -1 : 1,
				speed = this.cfg.speed * td.GRIDSIZE;
	
			if (Math.abs(distanceX) < speed && Math.abs(distanceY) < speed) {
				this.pX = this.nextGrid.pX;
				this.pY = this.nextGrid.pY;
			} else {
				this.pX += distanceX == 0 ? 0 : sx * speed;
				this.pY += distanceY == 0 ? 0 : sy * speed;
			}
		}
		this.setPosition(this.pX, this.pY);
	},
	
	beHit: function (weapon, damage) {
		this.hp -= damage;
		if (this.hp <= 0) {
			this.hpBar.setSize(0, this.hpBar.height);
			this.beKilled();
		}
		else
			this.hpBar.setSize(this.hp * this.hpBar.width /this.cfg.hp, this.hpBar.height);
	},
	
	beKilled: function () {
		this.hp = 0;
		this.destory();
		td.GAME.money += this.cfg.money;
		td.GAME.controller.updateMoney();
	}
});